<?php

namespace framework\support;

class Model
{
    /**
     * 根据id返回数据
     * @param $table
     * @param $id
     * @param bool $enableCache
     * @param string $keyField
     * @param string $deleteField
     * @return mixed
     */
    public static function getItemByID($table, $id, $enableCache = true, $keyField = 'id', $deleteField = "is_delete")
    {

        $cacheKey = "{$table}_{$id}";
        if ($enableCache) {
            if ($item = Redis::get($cacheKey)) {
                return igbinary_unserialize($item);
            }
        }
        if (!$keyField) {
            $keyField = 'id';
        }
        $where = [
            $keyField => $id
        ];
        if ($deleteField) {
            $where[$deleteField] = 0;
        }
        $item = Db::get($table, "*", $where);
        //检测是否被删除
        $isDelete = false;
        if (isset($item['is_delete'])) {
            $isDelete = $item['is_delete'] != 0;
        }
        if ($isDelete) {
            $item = null;
        }
        if ($item) {
            $item = (array)$item;
        }
        if ($enableCache && $item) {
            Redis::setEx($cacheKey, 1800, igbinary_serialize($item));
        }
        return $item;
    }

    /**
     * 获得一行数据
     * @param $where
     * @param $table
     * @param null $select
     * @return mixed
     */
    public static function getRow($table, $where, $select = "*", $join = null, $order = null)
    {
        if ($join) {
            return Db::get($table, $join, $select, $where, $order);
        } else {
            return Db::get($table, $select, $where);
        }
    }


    /**
     * 获得列表
     * @param $table
     * @param $where
     * @param int $page
     * @param int $size
     * @param null $select
     * @param null $order
     * @param array $joins
     * @param array $likes
     * @return mixed
     */
    public static function getList($table, $where, $page = 1, $size = 10, $select = "*", $order = null, $joins = [])
    {
        $page = intval($page);
        $size = intval($size);
        if ($page < 1) {
            $page = 1;
        }
        if ($size < 1) {
            $size = 10;
        }
        $start = ($page - 1) * $size;

        $options = [
            'LIMIT' => [$start, $size],
        ];
        if ($order) {
            $options['ORDER'] = $order;
        }
        if ($joins) {
            $list = Db::select($table, $joins, $select, $where, $options);
        } else {
            $list = Db::select($table, $select, $where, $options);
        }
        if ($joins) {
            $count = Db::count($table, $joins, "*", $where);
        } else {
            $count = Db::count($table, $where);
        }

        return [
            'list' => $list,
            'count' => $count,
        ];
    }

    /**
     * 获得列表
     * @param $table
     * @param array $where
     * @param null $select
     * @param null $order
     * @param array $joins
     * @param null $limit
     * @return mixed
     */
    public static function getAll($table, $where = [], $select = '*', $order = null, $joins = [], $limit = null)
    {

        $options = [];
        if ($limit) {
            $options['LIMIT'] = [$limit];
        }
        if ($order) {
            $options['ORDER'] = $order;
        }
        if ($joins) {
            $list = Db::select($table, $joins, $select, $where, $options);
        } else {
            $list = Db::select($table, $select, $where, $options);
        }
        return $list;
    }

    /**
     * 创建一个项目
     * @param $table
     * @param $data
     * @param null $key
     * @return int|string
     */
    public static function createItem($table, $data, $key = "id")
    {
        $id = Db::inster($table, $data);
        if (!isset($data[$key])) {
            return $id;
        } else {
            return $data[$key];
        }
    }

    /**
     * 更新一个项目
     * @param $table
     * @param $data
     * @return int|string
     */
    public static function updateItem($table, $id, $data, $key = "id", $cleanCache = true)
    {
        $num = Db::update($table, $data, [$key => $id]);
        if ($num) {
            if ($cleanCache) {
                Redis::del("{$table}_{$id}");
            }
            return $num;
        } else {
            return 0;
        }
    }

    /**
     * 创建一个项目
     * @param $table
     * @param $data
     * @param null $key
     * @return int|string
     */
    public static function updateOrInsert($table, $id, $data, $key = "id", $cleanCache = true)
    {
        $has = Db::has($table, [$key => $id]);
        if ($has) {
            $res = self::updateItem($table, $id, $data, $key, $cleanCache);
        } else {
            $res = self::createItem($table, $data, $key);
        }
        return $res;
    }

    /**
     * 删除指定记录
     * @param $table
     * @param $id
     * @return int
     */
    public static function delete($table, $id, $key = 'id', $cleanCache = true)
    {
        $res = Db::delete($table, [$key => $id]);
        if ($cleanCache) {
            Redis::del("{$table}_{$id}");
        }
        return  $res;
    }
}
